package prvi.wen.terp.controller;




import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import prvi.wen.terp.response.ResultResp;
import prvi.wen.terp.mapper.UserMapper;
import prvi.wen.terp.model.UserEntity;


@Controller
@RequestMapping("/user")
//@RestController
public class LoginController {
	

    @Autowired
    UserMapper mapper;

    @PostMapping("/login")
	public ResponseEntity<ResultResp> login(@RequestParam String name, @RequestParam String password){
        ResultResp result = new ResultResp();
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)){
            result.setMessage("用户名和密码不能为空");
            result.setCode("001001");
			return ResponseEntity.ok(result);
		}

        List<UserEntity> list = mapper.getByName(name);
        if(!CollectionUtils.isEmpty(list)){
            for(UserEntity userEntity :list){
                if(userEntity.getuPassword().equalsIgnoreCase(password)){
                    result.setData(userEntity);
                }else{
                	result.setMessage("账号或密码错误");
                	result.setCode("001002");
                }
            }
        }else{
            result.setMessage("数据库暂无用户记录！");
            result.setCode("001003");
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/dologin")
    public String login(@RequestParam String name, @RequestParam String password,ModelMap modelMap){
        ResultResp result = new ResultResp();
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)){
            modelMap.addAttribute("errmsg","用户名和密码不能为空");
        }


        List<UserEntity> list = new ArrayList<UserEntity>();
        list = mapper.getByName(name);
//        UserEntity aa = new UserEntity();
//        aa.setuName(name);
//        aa.setuPassword(password);
//        list.add(aa);
        if(!CollectionUtils.isEmpty(list)){
            for(UserEntity userEntity :list){
                if(userEntity.getuPassword().equalsIgnoreCase(password)){
                    result.setData(userEntity);
                    modelMap.addAttribute("user",userEntity);
                    return "index";
                }else{
                    modelMap.addAttribute("errmsg","账号或密码错误！");
                }
            }
        }else{
            modelMap.addAttribute("errmsg","数据库暂无用户记录！");
        }
        return "loginfo";
    }
    


    @GetMapping("/allUser")
    public ResponseEntity<ResultResp> allUser(){
        ResultResp result = new ResultResp();
        List<UserEntity> list = mapper.getAll();
        result.setData(list);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/addUser")
    public ResponseEntity addUser(@RequestParam String name, @RequestParam String password){
        ResultResp result = new ResultResp();
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)){
            result.setMessage("用户名和密码不能为空");
            result.setCode("001001");
            return ResponseEntity.ok(result);
        }
        List<UserEntity> list = mapper.getByName(name);
        if(!CollectionUtils.isEmpty(list)){
            result.setMessage("用户名已存在");
            result.setCode("001004");
            return ResponseEntity.ok(result);
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setCreatedBy("admin");
        userEntity.setCreatedDate(new Date());
        userEntity.setUpdatedBy("admin");
        userEntity.setUpdatedDate(new Date());
        userEntity.setLastLogin(new Date());
        userEntity.setRole(0);
        userEntity.setStatus(0);
        userEntity.setuName(name);
        userEntity.setuPassword(password);
        mapper.addUser(userEntity);
        result.setData(userEntity);
        return ResponseEntity.ok(result);
       
    }
}
    
